import pandas as pd
import numpy as np
# 加载数据
train = pd.read_csv('../数据集/train.csv')

# 加载 user_log 数据（假设文件已上传到指定路径）
user_log_df = pd.read_csv('../user_log.csv')


# 根据 user_id 和 merchant_id 合并两个数据表
merged_df = pd.merge( user_log_df,train, on=['user_id', 'merchant_id'])
print(merged_df.shape)

user_info_df = pd.read_csv('../数据集/user_info.csv')
merged_df = pd.merge(user_info_df,merged_df, on=['user_id'])

print(merged_df.head())
print(merged_df.shape)


print(merged_df['label'].value_counts())


print('数据基本信息：')
merged_df.info()

# 查看数据集行数和列数
print(merged_df.shape)

import matplotlib.pyplot as plt

# 计算label列的分布情况
class_distribution = merged_df['label'].value_counts()

print('label列的分布情况：')
print(class_distribution)

# 找出多数类和少数类及其数量
majority_class = class_distribution.idxmax()
minority_class = class_distribution.idxmin()
majority_count = class_distribution[majority_class]
minority_count = class_distribution[minority_class]

# 提取多数类样本
majority_samples = merged_df[merged_df['label'] == majority_class]

# 随机无放回地选择需要保留的多数类样本索引
samples_to_keep = majority_samples.sample(n=minority_count, replace=False, random_state=42).index

# 保留选中的多数类样本
filtered_majority = majority_samples.loc[samples_to_keep]

# 提取少数类样本
minority_samples = merged_df[merged_df['label'] == minority_class]

# 将筛选后的多数类样本与少数类样本合并
balanced_df = pd.concat([filtered_majority, minority_samples])

# 打印平衡后的类别分布
print('\n平衡后的label列分布情况：')
print(balanced_df['label'].value_counts())

csv_path = '../balanced_data.csv'
balanced_df.to_csv(csv_path, index=False)